package com.duan.laugh.mp.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.mp.api.pojo.entity.MpMenu;
import com.duan.laugh.mp.service.IMpMenuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 微信菜单管理
 *
 * @author duanjw
 * @date 2019-03-27 20:45:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxmenu")
public class MpMenuController {

    private final IMpMenuService IMpMenuService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param wxMpMenu
     * @return
     */
    @GetMapping("/page")
    public R getWxMpMenuPage(Page page, MpMenu wxMpMenu) {
        return R.ok(IMpMenuService.page(page, Wrappers.query(wxMpMenu)));
    }


    /**
     * 通过appid查询微信菜单
     *
     * @param appId 公众号
     * @return R
     */
    @GetMapping("/{appId}")
    public R getById(@PathVariable("appId") String appId) {
        return IMpMenuService.getByAppId(appId);
    }

    /**
     * 新增微信菜单
     *
     * @param wxMenu 微信菜单列表
     * @return R
     */
    @PostMapping("/{appId}")
    public R save(@RequestBody JSONObject wxMenu, @PathVariable String appId) {
        return R.ok(IMpMenuService.save(wxMenu, appId));
    }

    /**
     * 发布微信菜单
     *
     * @param appId 公众号
     * @return R
     */
    @PutMapping("/{appId}")
    public R updateById(@PathVariable String appId) {
        return IMpMenuService.push(appId);
    }

    @DeleteMapping("/{appId}")
    public R delete(@PathVariable("appId") String appId) {
        return IMpMenuService.delete(appId);
    }


}
